import geopandas as gpd
from utils.predata_utils import gen_matchrelation,process_df,gen_matchrelationUA,create_indictline_shapefile,add_shplen,classify_contours,add_Overlap
from math import atan2, degrees
import pandas as pd
from shapely.geometry import LineString, Point
import time
import os
import numpy as np
start_time = time.time()



""" 辅助人工匹配而进行的初步帅选"""

# # # 读入数据
# # # #加利福尼亚
# # contour1 = gpd.read_file('E:/Data/QGIS/2contour/Calif/ClfUg_Ct.shp' ,encoding='gbk')
# # contour2= gpd.read_file('E:/Data/QGIS/2contour/Calif/ClfDem_Ct.shp' ,encoding='gbk')
# # # 香港
# contour1 = gpd.read_file('E:/Data/QGIS/2contour/HG/HG2W_CtG.shp' ,encoding='gbk')
# contour2= gpd.read_file('E:/Data/QGIS/2contour/HG/HGDEM_CtG.shp' ,encoding='gbk')


# # # 生成匹配关系-距离
# # # # # 生成匹配关系，基于缓存区距离HG=49.11，Clf=21.03
# match_df = gen_matchrelation(contour2, contour1,160,"ele",overlap_threshold=0.2)
# # match_df = gen_matchrelationUA(contour2, contour1,14,overlap_threshold=0.01)
# match_df0=process_df(match_df)    

# # 生成真实匹配标记 
# true_pairs = pd.read_csv('data/HG_FMR.csv', header=0)
# true_pairs = true_pairs[true_pairs['Ture'] == 1]
# valid_pairs = set(zip(
#     true_pairs['col1'].astype(str), 
#     true_pairs['col2'].astype(str)
# ))
# col1 = match_df0['col1'].astype(str).values
# col2 = match_df0['col2'].astype(str).values
# match_df0['Ture'] = np.where([(c1, c2) in valid_pairs for c1, c2 in zip(col1, col2)], 1, 0)
# # 导出成csv文件
# match_df0.to_csv('dataCPR/HG_FMR0.csv',index=False)







""" 给csv增加shp属性 """

#读入数据
# contour1 = gpd.read_file('E:/Data/QGIS/2contour/HG/HG2W_CtG.shp' ,encoding='gbk')
# contour2= gpd.read_file('E:/Data/QGIS/2contour/HG/HGDEM_CtG.shp' ,encoding='gbk')
# df = pd.read_csv('data/HGMR_T.csv')
# # df_attr=add_Overlap(contour1,contour2,df,49.11) 
# df_attr=add_shplen(contour1,contour2,df) 
# df_attr.to_csv('data/HGMR_T0.csv', index=False, chunksize=2000)

################################################################################################################################################################################


""" 将等高线划分为开放式/闭合式等 """
# #读入数据
# ## 香港
# # HGpath1 = 'E:/Data/QGIS/2contour/HG/HG2W_Ct.shp' 
# # HGpath2= 'E:/Data/QGIS/2contour/HG/HGDEM_Ct.shp'
# ##加利福尼亚
# # Clfpath2='E:/Data/QGIS/2contour/Calif/ClfDem_Ct.shp'


# path1='E:/Data/QGIS/2contour/HG/HGDEM_Ct.shp'
# contour1 = gpd.read_file( path1,encoding='gbk')
# contour1_new=classify_contours(contour1)


# output_filename = os.path.splitext(os.path.basename(path1))[0] + 'G.shp'
# outputpath=os.path.join(os.path.dirname(path1), output_filename)
# contour1_new.to_file(outputpath,encoding='gbk') 



""" 生成指示线shp """
""" 需更改参数4个，3个 """
# 读入数据
# # #加利福尼亚
# contour1 = gpd.read_file('E:/Data/QGIS/2contour/Calif/对比结果示意图/area2_ug.shp' ,encoding='gbk')
# contour2= gpd.read_file('E:/Data/QGIS/2contour/Calif/对比结果示意图/area2_dem.shp' ,encoding='gbk')

# #香港
contour1 = gpd.read_file('E:/Data/QGIS/2contour/HG/对比结果示意图/area4_2w.shp' ,encoding='gbk')
contour2= gpd.read_file('E:/Data/QGIS/2contour/HG/对比结果示意图/area4_dem.shp' ,encoding='gbk')

crs=contour1.crs

new_match_df= pd.read_csv('data/HGDPC.csv')
# new_match_df= pd.read_csv('dataCPR/HGMR_fd.csv')
new_match_df0 = new_match_df[new_match_df['cluster'] == 1]
#cluster
indictL=create_indictline_shapefile(contour1,contour2,new_match_df0,crs)#
indictL.to_file('E:/Data/QGIS/2contour/HG/对比结果示意图/area4IL.shp')



# 运行时间
end_time = time.time()
print(f"代码运行时间：{end_time - start_time} 秒")